#include "formdocapply.h"
#include "ui_formdocapply.h"
#include <QDebug>
#include <QSettings>
#include <QTextCodec>
#include "pub_var.h"
#include <QMessageBox>

FormDocApply::FormDocApply(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormDocApply)
{
    ui->setupUi(this);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setIniCodec(QTextCodec::codecForName("UTF_8"));

    //恢复当前窗口位置
    QRect rct = setIni.value("windowState/docapply").toRect();
    this->setGeometry(rct);

    //数据库连接
    qrm = new QSqlQuery(pub::db_m);
    qro = new QSqlQuery(pub::db_o);

    ui->lbZsmc->setText("本机诊室：" + pub::dp_name);

    //医生名单
    qro->exec("select empl_name from com_employee where dept_code = '"
              + pub::dp_code + "' order by empl_code");
    ui->lwDocList->clear();
    while(qro->next()){
        ui->lwDocList->addItem(qro->value(0).toString());
    }

    ui->dtedRq->setDate(QDate::currentDate());
    ui->lwDocList->setFont(this->font());
    ui->lwSelected->setFont(this->font());
}

FormDocApply::~FormDocApply()
{
    delete ui;
}

void FormDocApply::on_dtedRq_dateChanged()
{
    qrm->prepare("select doc_name from doc_outpatient where op_date = :dt and doc_dp = :dp");
    qrm->bindValue(":dt",ui->dtedRq->date());
    qrm->bindValue(":dp",pub::dp_name);
    qrm->exec();
    ui->lwSelected->clear();
    while(qrm->next()){
        ui->lwSelected->addItem(qrm->value("doc_name").toString());
    }
}
void FormDocApply::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setIniCodec(QTextCodec::codecForName("UTF_8"));
    setIni.setValue("windowState/docapply", QVariant(this->geometry()));

}

void FormDocApply::on_pbExit_clicked()
{
    close();
}

void FormDocApply::on_pbSave_clicked()
{
    int ct = ui->lwSelected->count();

    if(ct == 0){
        QMessageBox::information(this,"注意","出诊医生选择无效！");
        return;
    }
    if(ui->dtedRq->date() < QDate::currentDate() ){
        QMessageBox::information(this,"注意","日期无效，不可以设置本日之前的日期！");
        return;

    }
    //删除原先记录
    qrm->prepare("delete from doc_outpatient where op_date = :dt and doc_dp = :dp");
    qrm->bindValue(":dt", ui->dtedRq->date());
    qrm->bindValue(":dp", pub::dp_name);
    qrm->exec();
    //插入当前选择
    while(ct>0){
        ct--;
        qrm->prepare("insert into doc_outpatient (op_date, doc_dp, doc_name ) values("
                     ":op_date, :doc_dp, :doc_name)");
        qrm->bindValue(":op_date", ui->dtedRq->date());
        qrm->bindValue(":doc_dp", pub::dp_name);
        qrm->bindValue(":doc_name", ui->lwSelected->item(ct)->text());
        qrm->exec();
    }
    QMessageBox::information(this,"注意",ui->dtedRq->date().toString("yyyy-MM-dd")+" 记录保存成功！");
}

void FormDocApply::on_lwDocList_itemDoubleClicked(QListWidgetItem *item)
{
    QList<QListWidgetItem *> lst = ui->lwSelected->findItems(item->text(),Qt::MatchExactly);

    if(lst.isEmpty()){
        ui->lwSelected->addItem(item->text());
    }
}

void FormDocApply::on_lwSelected_itemDoubleClicked(QListWidgetItem *item)
{
    ui->lwSelected->takeItem( ui->lwSelected->row(item) );
}

